<?php

namespace app\common\model;

use think\Model;

class Order extends Model
{
    // 表名
    protected $name = 'order';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    
    // 追加属性
    protected $append = [
        'create_time_text',
        'update_time_text',
        'shipping_time_text',
        'sign_time_text',
	    'status_text',
	    'shipping_text',
	    'comment_text'
    ];
    

    
    const STATUS_W = 0;
    const STATUS_S = 1;
    const STATUS_F = 2;
    
    public static $orderStatus = [
	    self::STATUS_F => '交易完成',
        self::STATUS_W => '待发货',
	    self::STATUS_S => '待签收',
    ];
	
	public function getStatusTextAttr($value, $data) {
		$value = $value ? $value : $data['order_status'];
		$list = self::$orderStatus;
		return isset($list[$value]) ? $list[$value] : '';
	}
	
	
	const SHIPPING_W = 0;
	const SHIPPING_S = 1;
	const SHIPPING_F = 2;
	
	public static $shippingStatus = [
		self::SHIPPING_F => '已签收',
		self::SHIPPING_S => '待签收',
		self::SHIPPING_W => '待发货',
	];
	
	public function getShippingTextAttr($value, $data) {
		$value = $value ? $value : $data['shipping_status'];
		$list = self::$orderStatus;
		return isset($list[$value]) ? $list[$value] : '';
	}
	
	const COMMENT_W = 0;
	const COMMENT_F = 1;
	
	public static $commentStatus = [
		self::COMMENT_F => '交易完成',
		self::COMMENT_W => '待发货',
	];
	
	public function getCommentTextAttr($value, $data) {
		$value = $value ? $value : $data['comment_status'];
		$list = self::$orderStatus;
		return isset($list[$value]) ? $list[$value] : '';
	}

    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getShippingTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['shipping_time']) ? $data['shipping_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getSignTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['sign_time']) ? $data['sign_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setShippingTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setSignTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    public function user(){
		return $this->belongsTo('user', 'user_id', 'id', '', 'left')->setEagerlyType(0);
    }
    
    public function goods(){
		return $this->belongsTo('goods', 'goods_id', 'id', '', 'left')->setEagerlyType(0);
    }

}
